<?php

class News extends Controller {

	var $uid;
	var $tpl;
    var $dbdata;

	function setVars()
	{
		$this->tpl['title'] = "Password Change Form";
		$this->tpl['head'] = '';
        $this->tpl['sidebar'] = '';
        $this->uid = $this->session->userdata('uid');
	}
	
	function __construct()
	{
		parent::Controller();
        $this->auth->CheckLogin();
		$this->setVars();
		log_message('debug', "News: Controler Class Initialized");
		//$this->output->enable_profiler(TRUE);
	}


	function index()
	{
		//$this->tpl['tpljs'][]  = '';
		$this->tpl['jsOnLoad']  = '';
		$this->tpl['title'] = "News Desk";
		$this->tpl['head'] = '';
        $this->tpl['sidebar'] = '';

		$query = $this->db->get('news');
		$this->load->helper('text');
		$this->tpl['news'] = '';
		foreach($query->result() as $row){ 
			$this->tpl['news'] .= "<h3>$row->Title</h3>";
			$this->tpl['news'] .= "<p>" . word_limiter($row->Body, 10) ."</p>";
			$this->tpl['news'] .= anchor('news/article/'.$row->id,'View Article');
			$this->tpl['news'] .= "<hr />";
		}

		$this->load->view('news/index', $this->tpl);
	}
	
	function article()
	{
		$this->load->helper('date');
		
		$format = 'r T';
		
		$this->db->where('id', $this->uri->segment(3));
		$query_news = $this->db->get('news');
		$row = $query_news->row();
		//$this->fun->print_a($row);
		$this->tpl['head'] = 'View Article';
		$this->tpl['title'] = $row->Title;
		$this->tpl['body'] = $row->Body;
		$this->tpl['aAuthor'] = $row->Author;
		$this->tpl['atime'] = $this->timetamer->ldate($row->Time);
		$row = $query_news->row();
		$this->db->where('mid', $this->uri->segment(3));
		$query_comments = $this->db->get('comments');
		if($query_comments->num_rows() > 0){
			$this->tpl['comments'] = '';
			foreach($query_comments->result() as $row){
				$this->tpl['comments'] .= "<p>$row->Body</p>";
				$ctime = $this->timetamer->ldate($row->Time);
				$this->tpl['comments'] .= "<p class=\"right\"><span class=\"author\">$row->Author</span><br \>";
				$this->tpl['comments'] .= "<span class=\"date\">$ctime</span></p><hr />";
			}
		} 		
		$this->load->view('news/article', $this->tpl);
	}

	function post()
	{
		$this->tpl['Title'] = '';
        $this->tpl['Author'] = '';
		$this->load->view('news/post', $this->tpl);
	}

	function post_insert()
	{
		$this->load->library('validation');
		$rules['Title']="trim|required|min_length[4]|max_length[40]|xss_clean";
		$rules['Body']  ="trim|required|min_length[9]|max_length[2000]|xss_clean";
		$rules['Author']="trim|required|min_length[4]|max_length[40]|xss_clean";
		
		$this->validation->set_rules($rules);
			
		if($this->validation->run() == FALSE)
		{
			$this->validation->set_error_delimiters('<div class="error">', 'test</div>');
		}
		else
		{
			$this->load->helper('date');
			foreach($_POST as $k => $v)
			{
				$post[$k] = trim($v); 
			}
			$post['Time'] = now();

			$this->db->insert('news', $post);
			redirect('news/article/'.$this->db->insert_id());
		}
	}
	
	function comment_insert()
	{
		$this->load->library('validation');
		$rules['Body']  ="trim|required|min_length[15]|max_length[120]|xss_clean";
		$rules['Author']="trim|required|min_length[4]|max_length[40]|xss_clean";
		
		$this->validation->set_rules($rules);
			
		if($this->validation->run() !== FALSE)
		{
			foreach($_POST as $k => $v)
			{
				$post[$k] = trim($v); 
			}
			$this->db->insert('comments', $post);
		}
		
		redirect('news/article/'.$_POST['mid']);
	}
}
?>
